<?php
if(!$_POST){
    include_once "./page_video2webp.tpl.html";
}else{
    $file = $_FILES['file']['tmp_name'];
    $fps = intval($_POST['fps']);
    $outWidth = intval($_POST['width']); // -vf scale=360:-1
    $quality = intval($_POST['quality']); // -q:v 75

    if(empty($file)){
        exit("请正确上传文件（文件体积超过限制？）");
    }
    //    file_put_contents("./input/".$_FILES['file'],file_get_contents($_FILES['tmp_name']));

    $outFileName = date('YmdHis')."-q{$quality}-w{$outWidth}-fps{$fps}.webp";
    $outFile = __DIR__."/".$outFileName;

    $q = $quality?'-q:v '.$quality : '';
    $scale = $outWidth?"-vf scale={$outWidth}:-1":'';

    $cmd = "ffmpeg -i {$file} -r {$fps} {$q} {$scale} -loop 0 -y {$outFile} 2>&1";
    exec($cmd ,$output);
//    var_dump($cmd,$output);

    header("Content-type:application/octet-stream");
    header("Accept-Ranges:bytes");
    header("Accept-Length:".filesize($outFile));
    header("Content-Disposition: attachment; filename=".$outFileName);
    echo file_get_contents($outFile);
}
